package com.example.coursedesign.ui.notifications.adapter;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.coursedesign.R;
import com.example.coursedesign.ui.dashboard.entity.Science;
import com.example.coursedesign.ui.home.entity.Medicine;


import java.util.ArrayList;
import java.util.List;

public class CollectAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int VIEW_TYPE_MEDICINE = 0;
    private static final int VIEW_TYPE_SCIENCE = 1;


    private List<Object> items = new ArrayList<>();

    // 设置药品列表
    public void setMedicines(List<Medicine> medicines) {
        this.items.clear();
        this.items.addAll(medicines);
        notifyDataSetChanged();
    }

    public void setSciences(List<Science> sciences) {
        this.items.clear();
        this.items.addAll(sciences);
        notifyDataSetChanged();
    }



    // 添加所有项目，根据类型区分
    public void addAllItems(List<?> items, boolean isMedicine, boolean isScience) {
        this.items.clear();
        if (isMedicine) {
            this.items.addAll((List<Medicine>) items);
        } else if (isScience) {
            this.items.addAll((List<Science>) items);
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        switch (viewType) {
            case VIEW_TYPE_MEDICINE:
                View medicineView = inflater.inflate(R.layout.item_medicine, parent, false);
                return new MedicineViewHolder(medicineView);
            case VIEW_TYPE_SCIENCE:
                View scienceView = inflater.inflate(R.layout.item_science, parent, false);
                return new ScienceViewHolder(scienceView);
            default:
                throw new IllegalArgumentException("Unknown view type: " + viewType);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Object item = items.get(position);

        if (holder instanceof MedicineViewHolder && item instanceof Medicine) {
            ((MedicineViewHolder) holder).bind((Medicine) item);
        } else if (holder instanceof ScienceViewHolder && item instanceof Science) {
            ((ScienceViewHolder) holder).bind((Science) item);
        }
    }

    @Override
    public int getItemViewType(int position) {
        Object item = items.get(position);
        if (item instanceof Medicine) {
            return VIEW_TYPE_MEDICINE;
        } else if (item instanceof Science) {
            return VIEW_TYPE_SCIENCE;
        }
            return -1; // 默认返回-1，表示未知类型
        }

        @Override
        public int getItemCount() {
            return items.size();
        }

        // MedicineViewHolder
        static class MedicineViewHolder extends RecyclerView.ViewHolder {

            private final TextView nameTextView;
            private final ImageView imageView;
            private final TextView descriptionTextView;

            public MedicineViewHolder(@NonNull View itemView) {
                super(itemView);
                nameTextView = itemView.findViewById(R.id.name);
                imageView = itemView.findViewById(R.id.imageView);
                descriptionTextView = itemView.findViewById(R.id.category);
            }

            public void bind(Medicine medicine) {
                nameTextView.setText(medicine.getName());
                descriptionTextView.setText(medicine.getCategory());
                if (medicine.getImage() != null && !medicine.getImage().isEmpty()) {
                    String base64Image = "";
                    if (medicine.getImage().contains(",")) {
                        base64Image = medicine.getImage().split(",", 2)[1];
                    } else {
                        base64Image = medicine.getImage();
                    }

                    byte[] imageBytes = Base64.decode(base64Image, Base64.DEFAULT);
                    Bitmap bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
                    imageView.setImageBitmap(bitmap);
                }
            }
        }
    static class ScienceViewHolder extends RecyclerView.ViewHolder {

        private final TextView titleTextView;
        private final ImageView imageView;
        private final TextView publishTimeTextView;
        private final TextView readCountTextView;

        public ScienceViewHolder(@NonNull View itemView) {
            super(itemView);
            titleTextView = itemView.findViewById(R.id.title);
            publishTimeTextView = itemView.findViewById(R.id.publishTime);
            readCountTextView = itemView.findViewById(R.id.readCount);
            imageView = itemView.findViewById(R.id.imageView);
        }

        public void bind(Science science) {
            titleTextView.setText(science.getTitle());
            publishTimeTextView.setText(science.getPublish_time());
//            readCountTextView.setText(science.getRead_count());
            readCountTextView.setText("阅读量："+String.valueOf(science.getRead_count()));
            if (science.getImage() != null && !science.getImage().isEmpty()) {
                String base64Image = "";
                if (science.getImage().contains(",")) {
                    base64Image = science.getImage().split(",", 2)[1];
                } else {
                    base64Image = science.getImage();
                }

                byte[] imageBytes = Base64.decode(base64Image, Base64.DEFAULT);
                Bitmap bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
                imageView.setImageBitmap(bitmap);
            }
        }
    }
    }
